<template>
	<BaseContainer>
		<aplid-base-crud
			ref="crud"
			class="w-100"
			v-bind="{ ...baseConfig, ...$attrs }"
			:searchConfig="!showSearchConfig ? false : searchConfig"
			:formConfig="formConfig"
			:operateBefore="operateBefore"
			:saveFormate="saveFormate"
		>
			<template v-slot:errorInfo="{ row }">
				<span class="text-red">{{ row.errorInfo }}</span>
			</template>
			<template v-slot:multipleAppealDialog="{ show }">
				<MultipleAppeal :fromType="1" v-if="show" :multipleSelection="multipleSelectList" @refresh="handleRefresh"></MultipleAppeal>
			</template>
		</aplid-base-crud>
	</BaseContainer>
</template>

<script setup lang="tsx">
import { baseConfig, apiOrder, searchConfig, appealFormConfig } from './config'
import MultipleAppeal from './MultipleAppeal/index.vue'
import { ElMessage } from 'element-plus'
import apiMod from '@/apiMod/index'

withDefaults(
	defineProps<{
		showSearchConfig?: boolean
	}>(),
	{
		showSearchConfig: true,
	}
)
const formConfig = ref(baseConfig.formConfig)
const operateType = ref('')
const crud = ref(null)
const multipleSelectList = ref([])
const changeBefore = ref({})
const saveFormate = async (form) => {
	return new Promise((resolve, reject) => {
		if (operateType.value == 'approval') {
			resolve({
				handleStatus: form.handleStatus,
				handleStatusLabel: form.handleStatusLabel,
				idList: [form.id],
			})
		} else {
			form.fromId = form.orderId||form.fromId
			form.fromType = 1
			resolve(form)
		}
	})
}
const handleRefresh = () => {
	nextTick(() => {
		crud.value?.refreshChange({})
	})
}

const operateBefore = (form, type) => {
  operateType.value = type 
	return new Promise((resolve, reject) => {
		const multipleSelection = crud.value?.multipleSelection
		multipleSelectList.value = multipleSelection
		formConfig.value = type == 'approval' ? appealFormConfig : baseConfig.formConfig

		if (type == 'multipleAppeal' && multipleSelection.length == 0) {
			ElMessage.warning('请选择申诉项')
			return
		} 
    // else if (type == 'approval') {
		// 	apiMod.view_omAppeal_view(form.id).then((res) => {
		// 		resolve({
		// 			...res.data,
		// 			...form
		// 		})
		// 	})
		// 	return
		// }
		resolve(form)
	})
}
</script>

<style lang="scss" scoped></style>
